package com.cloud.elm.controller;

import com.cloud.elm.po.User;
import com.cloud.elm.po.dto.UserDTO;
import com.cloud.elm.service.BusinessService;
import com.cloud.elm.service.UserService;
import com.cloud.elm.utils.CommonUtil;
import com.cloud.elm.utils.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;


@RestController
@RequestMapping("/elm/UserController")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private BusinessService businessService;

//	@Autowired
//	private BCryptPasswordEncoder encoder;

	@GetMapping("/getUserByIdByPass/{userId}/{password}")
	public ResultBody<User> getUserByIdByPass(@PathVariable("userId") String userId,
											  @PathVariable("password") String password) {
		String encodedPwd = userService.getPasswordById(userId);
		if (Objects.equals(encode(password), encodedPwd)) {
			User user1 = userService.getUserInfoById(userId);
			user1.setPassword("unknown");
			return ResultBody.success(user1);
		}
		return ResultBody.error();
	}

	@GetMapping("/user/info/{userId}")
	public ResultBody<User> getUserInfoById(@PathVariable("userId") String userId) throws Exception{
		try {
			User user = userService.getUserInfoById(userId);
			if (user == null) {
				return ResultBody.error("获取失败！", null);
			}
			user.setPassword("unknown");
			return ResultBody.success("获取成功！", user);
		} catch (Exception e) {
			return ResultBody.error("获取失败！", null);
		}
	}

	@GetMapping("/getUserById/{userId}")
	public ResultBody<Integer> getUserById(@PathVariable("userId") String userId) {
		int userCnt = userService.getUserById(userId);
		if (userCnt > 0) {
			return ResultBody.success("获取成功！", userCnt);
		}
		return ResultBody.error("不存在用户！", userCnt);
	}

	@PostMapping("/saveUser/{userId}/{password}/{userName}/{userSex}")
	public ResultBody<Integer> saveUser(@PathVariable("userId") String userId,
										@PathVariable("password") String password,
										@PathVariable("userName") String userName,
										@PathVariable("userSex") Integer userSex) {
		if (userService.getUserById(userId) == 0) {
			User user = User.builder()
					.userId(userId)
					.password(encode(password))
					.userName(userName)
					.userSex(userSex).build();
			return ResultBody.success("保存成功！", userService.saveUser(user));
		} else {
			return ResultBody.error("用户已存在！", null);
		}
	}

	@PutMapping("/changePassword/{userId}/{oldPassword}/{newPassword}")
	public ResultBody<Object> changePassword(@PathVariable("userId") String userId,
											 @PathVariable("oldPassword") String oldPassword,
											 @PathVariable("newPassword") String newPassword){
		try {
			String password = userService.getPasswordById(userId);
			if (!Objects.equals(encode(oldPassword), password)) {
				return ResultBody.error("原始密码错误", null);
			}
			newPassword = encode(newPassword);
			userService.changePassword(userId, newPassword);
			return ResultBody.success("修改密码成功！", null);
		} catch (Exception e){
			return ResultBody.error("修改失败！", null);
		}
	}

	@PutMapping("/user/updateInfo/{userId}/{userName}/{userImg}")
	public ResultBody<Object> updateInfo (@PathVariable("userId") String userId,
										  @PathVariable("userName") String userName,
										  @PathVariable("userImg") String userImg) {
		try {
			int userCnt = userService.getUserById(userId);
			if (userCnt == 0) {
				return ResultBody.error("不存在该用户！", null);
			}
			User user = userService.getUserInfoById(userId);
			UserDTO userDTO = new UserDTO(userId, userName, userImg);
			userService.updateInfo(userDTO);

			if (Objects.equals(user.getRole(), CommonUtil.ROLE_BUSINESS)) {
				businessService.updateBusinessById(userId, userName, userImg);
			}
			return ResultBody.success("修改成功！", null);
		} catch (Exception e) {
			return ResultBody.error("修改失败！", null);
		}
	}

	private String encode(String password) {
		return DigestUtils.md5DigestAsHex(password.getBytes());
	}
}
